package xyz.ranyh.jobclient.http;

import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONObject;
import org.json.JSONTokener;

import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by rany on 1/3/17.
 */

public class StarJobTask extends AsyncTask<Void, Void, JSONObject> {
    OkHttpClient client = new OkHttpClient();
    int jobId;
    String url;

    public StarJobTask(int id, String url) {
        jobId = id;
        this.url = url;
    }

    @Override
    protected JSONObject doInBackground(Void... params) {
        try {
            String credential = Credentials.basic(Resource.getInstance().token, "");
            Request request =  new Request.Builder()
                    .url(url+jobId)
                    .addHeader("Authorization", credential)
                    .addHeader("accept", "application/json")
                    .build();
            Response response = client.newCall(request).execute();

            String responseText = response.body().string();

            JSONTokener resJson = new JSONTokener(responseText);
            JSONObject bodyJson = (JSONObject) resJson.nextValue();

            return bodyJson;

        } catch (Exception e) {
            Log.d("Register task", e.getMessage());
        }
        return null;
    }
}
